package cn.itsource.org.service.impl;

import cn.itsource.basic.query.BaseQuery;
import cn.itsource.basic.query.PageList;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.mapper.EmployeeMapper;
import cn.itsource.org.service.IEmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee,BaseQuery> implements IEmployeeService {

    //注入Mapper层
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public List<Employee> getAll() {
        return employeeMapper.selectAll();
    }

    @Override
    public PageList<Employee> getAllByPage(BaseQuery baseQuery) {
        //1.传递分页的参数
        PageHelper.startPage(baseQuery.getPageNum(),baseQuery.getPageSize());

        //2.执行查询操作，查询所有
        List<Employee> employees = employeeMapper.selectAll();

        //3.使用分页工具类封装分页参数
        PageInfo<Employee> pageInfo = new PageInfo<>(employees);

        //4.使用自己的分页工具类封装数据
        PageList<Employee> pageListl = new PageList<>(pageInfo.getTotal(), pageInfo.getList());
        //5.返回数据
        return pageListl;
    }

    @Override
    public void saveOrUpdate(Employee employee) {
        if(employee.getId() == null){
            employeeMapper.insertSelective(employee);
        }else{
            employeeMapper.updateByPrimaryKeySelective(employee);
        }

    }

    @Override
    public void delete(Long id) {
        employeeMapper.deleteByPrimaryKey(id);

    }

    @Override
    public List<Employee> getManages() {
        return employeeMapper.getManages();
    }
}
